#include "Canon_White.h"

CText* CCanon_White::text_ = NULL;

CCanon_White::CCanon_White()
{
	this->position_			= D3DXVECTOR3();
	this->velocity_			= D3DXVECTOR3();
	this->currentFrame_		= 0;
	this->currentStatus_	= wcStatus::WHITE_CLOSE;
	loadSprites();
}

CCanon_White::CCanon_White(D3DXVECTOR3 position)
{
	this->position_ = position;
	this->velocity_ = D3DXVECTOR3(0,0,0);
	this->currentFrame_ = 0;
	this->currentStatus_ = wcStatus::WHITE_CLOSE;
	this->isClosed_ = false;
	this->isOpened_ = false;
	this->isShooting_ = false;
	this->count_ = 0;
	loadSprites();
}

CCanon_White::~CCanon_White()
{
}

void CCanon_White::Update(DWORD deltaTime)
{
	currentFrame_++;
	if (isOpened_ && currentFrame_ == 3)
		isShooting_ = true;
	currentFrame_ %= 3;
}
void CCanon_White::Render()
{
	spriteVector_[currentStatus_]->Render(0, -1, position_);

	//print to screen some infomation
	text_->Draw(CText::tostrW(position_.x), *CConvert::createRect(350, 400, 300, 360), DT_LEFT, D3DCOLOR_XRGB(255, 255, 255));
	text_->Draw(CText::tostrW((float)currentStatus_),*CConvert::createRect(300,360,300,360),DT_LEFT,D3DCOLOR_XRGB(255,255,255));

}

void CCanon_White::loadSprites()
{
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_1.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_2.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_3.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_4.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_5.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_6.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_7.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_8.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_9.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_10.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_11.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_12.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_OPEN.png", 1, 3, 3));
	spriteVector_.push_back(new CSprite(L"WhiteCanon//WHITE_CLOSE.png", 1, 3, 3));
}

void CCanon_White::AI(D3DXVECTOR3 playerPosition)
{
	if (playerPosition.x > position_.x - 100 && !isOpened_ && !isShooting_ &&!isClosed_)
	{
		setStatus(wcStatus::WHITE_OPEN);
		this->isOpened_ = true;
	}
	if (this->isOpened_)
	{
		if (changeStatus(wcStatus::WHITE_OPEN))
		{
			this->isShooting_ = true;
			this->currentStatus_ = WHITE_7;
			this->isOpened_ = false;
		}
	}
	if (isShooting_)
	{
		int temp = abs(playerPosition.x - position_.x) / 300;
		int temp2 = abs(playerPosition.x - position_.x) / 100;
		int temp3 = abs(playerPosition.x - position_.x) / 50;
		int temp4 = abs(playerPosition.x - position_.x) / 25;
		if (temp > 0)
		{
			if (playerPosition.x > position_.x)
			{
				setStatus(WHITE_CLOSE);
				this->isShooting_ = false;
				this->isClosed_ = true;
			}
			else
				setStatus(WHITE_9);
		}
		else
			if (temp2 > 0)
			{
				if (playerPosition.y < position_.y)
					moveStatus(playerPosition.x > position_.x ? wcStatus::WHITE_2 : wcStatus::WHITE_10);
				else
					moveStatus(playerPosition.x > position_.x ? wcStatus::WHITE_4 : wcStatus::WHITE_8);
			}
			else
				if (temp3 > 0)
				{
					if (playerPosition.y < position_.y)
						moveStatus(playerPosition.x > position_.x ? wcStatus::WHITE_1 : wcStatus::WHITE_11);
					else
						moveStatus(playerPosition.x > position_.x ? wcStatus::WHITE_5 : wcStatus::WHITE_7);
				}
				else
					if (temp4)
					{
						if (playerPosition.y < position_.y)
							moveStatus(wcStatus(WHITE_12));
						else
							moveStatus(wcStatus(WHITE_6));
					}
	}
	if (isClosed_)
	{
		changeStatus(WHITE_CLOSE);
	}
}

void CCanon_White::setStatus(wcStatus status)
{
	if (currentStatus_ != status)
	{
		currentStatus_ = status;
	}
}

bool CCanon_White::moveStatus(wcStatus status)
{
	if (currentStatus_ == status)
		return true;
	count_++;
	if (count_ == SEC_MOVE)
	{
		count_ = 0;
		if (currentStatus_ >= WHITE_10 && status <= WHITE_2)
		{
			if(currentStatus_ == WHITE_12)
				setStatus(WHITE_1);
			else
				setStatus(NEXT_STATUS);
			return false;
		}
		if (status >= WHITE_10 && currentStatus_ <= WHITE_2)
		{
			if (currentStatus_ == WHITE_1)
				setStatus(WHITE_12);
			else
				setStatus(BACK_STATUS);
			return false;
		}
		setStatus(currentStatus_ < status ? NEXT_STATUS : BACK_STATUS);
	}
	return false;
}

bool CCanon_White::changeStatus(wcStatus status)
{
	count_++;
	if (count_ == SEC_MOVE)
	{
		setStatus(status);
		count_ = 0;
		return true;
	}
	return false;
}

void CCanon_White::setText(CText* text)
{
	text_ = text;
}